/*
 * SimpleSink.cpp
 *
 *  Created on: 2011-03-02
 *      Author: Jagger
 */

#include <fstream>
#include <locale>
#include "SimpleSink.h"
#include "NetPacket_m.h"

extern std::ofstream out;

Define_Module(SimpleSink)

void SimpleSink::initialize()
{
	numReceived = 0;
	WATCH(numReceived);
}

void SimpleSink::handleMessage(cMessage* msg)
{
	if(!msg->isSelfMessage())
	{
		EV << "Received \"" << msg->getName() << "\" message.\n";
		numReceived++;

		NetPacket* pck = check_and_cast<NetPacket*>(msg);

		char buf[512];
		sprintf(buf, "%d;%d;%d;%.3f;%.3f;1\n", pck->getPacketId(), pck->getPriority(), (int32_t)pck->getByteLength(), pck->getMakeTime().dbl(), simTime().dbl());
		out.write(buf, strlen(buf));

		pck->setLeftSuccessfully(true);

		delete msg;
	}
}
